home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15605 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: news.mindlink.net!news
  2. From: Allan_Nienhuis@mindlink.bc.ca (Allan Nienhuis)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Dynamically allocating 2-dim arrays
  5. Date: Sun, 07 Apr 1996 13:17:46 GMT
  6. Organization: MIND LINK! - British Columbia, Canada
  7. Message-ID: <4k8f3t$ium@fountain.mindlink.net>
  8. References: <4k47ii$ppa@galaxy.ucr.edu>
  9. NNTP-Posting-Host: line007.abb.mindlink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. csamaras@ucrengr.ucr.edu (Constantine Samares) wrote:
  13.  
  14. >   Does anyone out there know the "correct" way of dynamically allocating a
  15. >2-dimensional array in C++? This is the problem I'm running into:
  16.  
  17. As far as I know, it can't be done without using a custom class.
  18.  
  19. Why don't you just use pointer arithmetic instead?  it's generally
  20. much faster, anyways.
  21.  
  22. eg:
  23.  
  24. int *PtrTo_int_array;
  25. int size_of_x_dimension = 100;
  26. int size_of_y_dimension = 500;
  27.  
  28. PtrTo_int_array = new int[size_of_x_dimension * size_of_y_dimension];
  29.  
  30. // access the element like this:
  31. int x = 50;
  32. int y = 75;
  33.  
  34. int element = *(PtrTo_int_array + (x * size_of_y_dimension) + y);
  35.  
  36.  
  37.  
  38. Or something like that....   :)
  39.  
  40.  
  41.